package com.alibaba.citrus.cr.platform.enhance.facade.service.role;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.platform.enhance.functions.role.RoleEntity;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.EmployeeGetCurrentLoginResponse;
import com.epoch.app.bcorder.model.dto.RoleSDO;
import com.epoch.app.bcorder.service.BcOrderService;
import com.epoch.app.crplatformenhance.enums.RoleQueryTypeEnum;
import com.epoch.app.crplatformenhance.rolewrite.dto.CreateRoleToPageRequest;
import com.epoch.app.crplatformenhance.rolewrite.dto.EditRoleToPageRequest;
import com.epoch.app.crplatformenhance.rolewrite.dto.RemoveRoleToPageRequest;
import com.epoch.app.crplatformenhance.rolewrite.service.RoleWriteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2022/11/30 22:15
 */
@Component
@Primary
public class RoleWriteServiceImpl implements RoleWriteService {

    @Resource
    private BcOrderService bcOrderService;

    @Override
    @FacadeInvoker
    public Result<String> createRoleToPage(CreateRoleToPageRequest createRoleToPageRequest) {
        EmployeeGetCurrentLoginResponse loginUser = bcOrderService.employeeGetCurrentLogin();
        RoleSDO roleSDO = new RoleSDO();
        roleSDO.setName(createRoleToPageRequest.getName());
        roleSDO.setDescription(createRoleToPageRequest.getDescription());
        roleSDO.setPermissionIdList(createRoleToPageRequest.getPermissionIdList());
        if(StringUtils.isBlank(createRoleToPageRequest.getType())){
            throw new FacadeException(ErrorMessage.code("OTS-01-001-02-15-003"));
        }
        RoleQueryTypeEnum roleQueryTypeEnum = RoleQueryTypeEnum.of(createRoleToPageRequest.getType());
        roleSDO.setBelongOrganizationId(roleQueryTypeEnum != null?roleQueryTypeEnum.getBelongOrganizationId():loginUser.getBelongOrganizationId());
        roleSDO.setOperatorId(loginUser.getEmployeeId());
        roleSDO.setGroup(createRoleToPageRequest.getGroup());
        RoleEntity roleEntity = RoleEntity.of(roleSDO);
        return roleEntity.doCreate();
    }

    @Override
    @FacadeInvoker
    public Result<Boolean> editRoleToPage(EditRoleToPageRequest editRoleToPageRequest) {
        EmployeeGetCurrentLoginResponse loginUser = bcOrderService.employeeGetCurrentLogin();
        RoleSDO roleSDO = new RoleSDO();
        roleSDO.setId(editRoleToPageRequest.getId());
        roleSDO.setName(editRoleToPageRequest.getName());
        roleSDO.setDescription(editRoleToPageRequest.getDescription());
        roleSDO.setPermissionIdList(editRoleToPageRequest.getPermissionIdList());
        if(StringUtils.isBlank(editRoleToPageRequest.getType())){
            throw new FacadeException(ErrorMessage.code("OTS-01-001-02-15-003"));
        }
        RoleQueryTypeEnum roleQueryTypeEnum = RoleQueryTypeEnum.of(editRoleToPageRequest.getType());
        roleSDO.setBelongOrganizationId(roleQueryTypeEnum != null?roleQueryTypeEnum.getBelongOrganizationId():loginUser.getBelongOrganizationId());
        roleSDO.setOperatorId(loginUser.getEmployeeId());
        roleSDO.setGroup(editRoleToPageRequest.getGroup());
        RoleEntity roleEntity = RoleEntity.of(roleSDO);
        return roleEntity.doEdit();
    }

    @Override
    @FacadeInvoker
    public Result<Boolean> removeRoleToPage(RemoveRoleToPageRequest removeRoleToPageRequest) {
        EmployeeGetCurrentLoginResponse loginUser = bcOrderService.employeeGetCurrentLogin();
        RoleSDO roleSDO = new RoleSDO();
        roleSDO.setId(removeRoleToPageRequest.getId());
        RoleQueryTypeEnum roleQueryTypeEnum = RoleQueryTypeEnum.of(removeRoleToPageRequest.getType());
        roleSDO.setBelongOrganizationId(roleQueryTypeEnum != null?roleQueryTypeEnum.getBelongOrganizationId():loginUser.getBelongOrganizationId());
        RoleEntity roleEntity = RoleEntity.of(roleSDO);
        return roleEntity.doRemove();
    }

}
